home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ For TASM / USRGUIDE.PAK / HEX.ASM < prev    next >
Assembly Source File  |  1996-02-21  |  2KB  |  64 lines

  1. ; Turbo Assembler example. Copyright (c) 1993 By Borland International, Inc.
  2. ;
  3. ; HEX.ASM
  4. ;
  5. ; Usage: Run tasm on this file and link with hex.pas
  6.  
  7. CODE      SEGMENT
  8.           ASSUME cs:CODE,ds:NOTHING
  9.  
  10. ; Parameters (+2 because of push bp)
  11.  
  12. byteCount EQU BYTE PTR  ss:[bp+6]
  13. num       EQU DWORD PTR ss:[bp+8]
  14.  
  15. ; Function result address (+2 because of push bp)
  16.  
  17. resultPtr EQU DWORD PTR ss:[bp+12]
  18.  
  19.  
  20. HexStr    PROC FAR
  21.           PUBLIC HexStr
  22.  
  23.           push bp
  24.           mov bp,sp          ;get pointer into stack
  25.           les di,resultPtr   ;get address of function result
  26.           mov dx,ds          ;save Turbo's DS in DX
  27.           lds si,num         ;get number address
  28.           mov al,byteCount   ;how many bytes?
  29.           xor ah,ah          ;make a word
  30.           mov cx,ax          ;keep track of bytes in CX
  31.           add si,ax          ;start from MS byte of number
  32.           dec si
  33.           shl ax,1           ;how many digits? (2/byte)
  34.           cld                ;store # digits (going forward)
  35.           stosb              ;in destination string's length byte
  36. HexLoop:
  37.           std                ;scan number from MSB to LSB
  38.           lodsb              ;get next byte
  39.           mov ah,al          ;save it
  40.           shr al,1           ;extract high nibble
  41.           shr al,1
  42.           shr al,1
  43.           shr al,1
  44.           add al,90h         ;special hex conversion sequence
  45.           daa                ;using ADDs and DAA's
  46.           adc al,40h
  47.           daa                ;nibble now converted to ASCII
  48.           cld                ;store ASCII going up
  49.           stosb
  50.           mov al,ah          ;repeat conversion for low nibble
  51.           and al,0Fh
  52.           add al,90h
  53.           daa
  54.           adc al,40h
  55.           daa
  56.           stosb
  57.           loop HexLoop       ;keep going until done
  58.           mov ds,dx          ;restore Turbo's DS
  59.           pop bp
  60.           ret 6              ;parameters take 6 bytes
  61. HexStr    ENDP
  62. CODE      ENDS
  63.           END
  64.